package com.ruoyi.app.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.app.domain.bo.AppGoodsBo;
import com.ruoyi.app.domain.vo.AppGoodsVo;
import com.ruoyi.app.domain.AppGoods;
import com.ruoyi.app.mapper.AppGoodsMapper;
import com.ruoyi.app.service.IAppGoodsService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * APP商城商品Service业务层处理
 *
 * @author heike
 * @date 2023-02-28
 */
@RequiredArgsConstructor
@Service
public class AppGoodsServiceImpl implements IAppGoodsService {

    private final AppGoodsMapper baseMapper;

    /**
     * 查询APP商城商品
     */
    @Override
    public AppGoodsVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询APP商城商品列表
     */
    @Override
    public TableDataInfo<AppGoodsVo> queryPageList(AppGoodsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AppGoods> lqw = buildQueryWrapper(bo);
        Page<AppGoodsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询APP商城商品列表
     */
    @Override
    public List<AppGoodsVo> queryList(AppGoodsBo bo) {
        LambdaQueryWrapper<AppGoods> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AppGoods> buildQueryWrapper(AppGoodsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AppGoods> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getGoodsName()), AppGoods::getGoodsName, bo.getGoodsName());
        lqw.eq(StringUtils.isNotBlank(bo.getGoodsDetail()), AppGoods::getGoodsDetail, bo.getGoodsDetail());
        lqw.eq(StringUtils.isNotBlank(bo.getGoodsDescribe()), AppGoods::getGoodsDescribe, bo.getGoodsDescribe());
        lqw.eq(StringUtils.isNotBlank(bo.getGoodsInventory()), AppGoods::getGoodsInventory, bo.getGoodsInventory());
        lqw.eq(bo.getGoodsPrice() != null, AppGoods::getGoodsPrice, bo.getGoodsPrice());
        lqw.eq(bo.getClassId() != null, AppGoods::getClassId, bo.getClassId());
        lqw.eq(StringUtils.isNotBlank(bo.getBuyType()), AppGoods::getBuyType, bo.getBuyType());
        lqw.eq(StringUtils.isNotBlank(bo.getSendType()), AppGoods::getSendType, bo.getSendType());
        lqw.eq(StringUtils.isNotBlank(bo.getGoodsTransfer()), AppGoods::getGoodsTransfer, bo.getGoodsTransfer());
        lqw.eq(bo.getTransferFee() != null, AppGoods::getTransferFee, bo.getTransferFee());
        lqw.eq(StringUtils.isNotBlank(bo.getImages()), AppGoods::getImages, bo.getImages());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), AppGoods::getStatus, bo.getStatus());
        lqw.eq(bo.getSellNum() != null, AppGoods::getSellNum, bo.getSellNum());
        lqw.eq(bo.getLikeNum() != null, AppGoods::getLikeNum, bo.getLikeNum());
        return lqw;
    }

    /**
     * 新增APP商城商品
     */
    @Override
    public Boolean insertByBo(AppGoodsBo bo) {
        AppGoods add = BeanUtil.toBean(bo, AppGoods.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改APP商城商品
     */
    @Override
    public Boolean updateByBo(AppGoodsBo bo) {
        AppGoods update = BeanUtil.toBean(bo, AppGoods.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AppGoods entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除APP商城商品
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Boolean downWithValidByIds(Collection<Long> ids) {
        ids.forEach(e->{


        });
        return null;
    }
}
